package com.qdu.servlet;


import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.qdu.dao.UserLoginDao;

public class RegUtil extends HttpServlet {

	public final static String CONTENTTYPE="text/html;charset=UTF-8";
	public final static String CHARACTERENCODING="UTF-8";

    public RegUtil() {
        super();
    }
   public void destroy() {
        super.destroy(); 
    }
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding(this.CHARACTERENCODING);
        response.setContentType(this.CONTENTTYPE);
        String method = request.getParameter("method").trim();//获取method并将string类型数据的前后端空格去掉
        UserLoginDao ub = new UserLoginDao();//实体类
//注册用户
        if (method.equals("reg")) {//如果获取的method等于reg
            //获取以下数据
            String login = request.getParameter("login").trim();
            String password = request.getParameter("password").trim();
            String name = request.getParameter("name").trim();
            String sex = request.getParameter("sex").trim();
            String email = request.getParameter("email").trim();
            String tel = request.getParameter("tel").trim();
            if (login.equals("") || password.equals("") || name.equals("") || email.equals("") || tel.equals("")) {//判空
                request.setAttribute("login", login);
                request.setAttribute("password", password);
                request.setAttribute("name", name);
                request.setAttribute("email", email);
                request.setAttribute("tel", tel);
                request.setAttribute("message", "必须填写所有项目！");
                request.getRequestDispatcher("reg.jsp").forward(request, response);//打包数据
            } else {
                try {
                    int t = Integer.parseInt(tel);//封装int类

                    int i = ub.regUser(login, password, name, sex, email, tel);//新用户注册，dao类方法
                    if (i == 1) {//注册成功
                        HttpSession session = request.getSession();
                        session.setAttribute("name", login);
                        request.setAttribute("message", "注册成功！");
                        request.getRequestDispatcher("center.jsp").forward(request, response);
                    } else if (i == 3) {//有重名
                        request.setAttribute("login", login);
                        request.setAttribute("password", password);
                        request.setAttribute("name", name);
                        request.setAttribute("email", email);
                        request.setAttribute("tel", tel);
                        request.setAttribute("message", "该用户名已经存在，请重新选择用户名！");
                        request.getRequestDispatcher("reg.jsp").forward(request, response);
                    } else {//系统错误
                        request.setAttribute("login", login);
                        request.setAttribute("password", password);
                        request.setAttribute("name", name);
                        request.setAttribute("email", email);
                        request.setAttribute("tel", tel);
                        request.setAttribute("message", "系统维护中，请稍后再试！");
                        request.getRequestDispatcher("reg.jsp").forward(request, response);
                    }
                } catch (Exception e) {//捕获异常
                    request.setAttribute("login", login);
                    request.setAttribute("password", password);
                    request.setAttribute("name", name);
                    request.setAttribute("email", email);
                    request.setAttribute("tel", tel);
                    request.setAttribute("message", "电话号码不合规则！");
                    request.getRequestDispatcher("reg.jsp").forward(request, response);
                }
            }
        } 
//找回密码
        else if (method.equals("lost")) {
            String login = request.getParameter("login").trim();
            String email = request.getParameter("email").trim();
            int i = ub.getMiMa(login, email);
            if (i == 1) {//找回成功
                request.setAttribute("message", "密码成功找回，您的新密码为123456，请登陆后尽快修改！");
                request.getRequestDispatcher("index.jsp").forward(request, response);
            } else  if (i == 3) {//错误
                request.setAttribute("message", "登陆名或邮箱错误！");
                request.getRequestDispatcher("lost.jsp").forward(request, response);
            } else if (i == 2) {//insert出现问题
                request.setAttribute("message", "系统维护中，请稍后再试！");
                request.getRequestDispatcher("lost.jsp").forward(request, response);
            }
        } 
//修改指定用户名称的普通用户信息        
            else if (method.equals("up")) {
            String login = request.getParameter("login").trim();
            String password = request.getParameter("password").trim();
            String name = request.getParameter("name").trim();
            String sex = request.getParameter("sex").trim();
            String email = request.getParameter("email").trim();
            String tel = request.getParameter("tel").trim();
            if (login.equals("") || password.equals("") || name.equals("") || email.equals("") || tel.equals("")) {//判空
                request.setAttribute("login", login);
                request.setAttribute("password", password);
                request.setAttribute("name", name);
                request.setAttribute("email", email);
                request.setAttribute("tel", tel);
                request.setAttribute("message", "必须填写所有项目！");
                request.getRequestDispatcher("upuser.jsp").forward(request, response);
            } else {
                try {
                    int t = Integer.parseInt(tel);
                    int i = ub.upUser(login, password, name, sex, email, tel);
                    if (i == 1) {
                        request.setAttribute("message", "操作成功！");
                        request.getRequestDispatcher("upuser.jsp").forward(request, response);
                    } else {
                        request.setAttribute("login", login);
                        request.setAttribute("password", password);
                        request.setAttribute("name", name);
                        request.setAttribute("email", email);
                        request.setAttribute("tel", tel);
                        request.setAttribute("message", "系统维护中，请稍后再试！");
                        request.getRequestDispatcher("upuser.jsp").forward(request, response);
                    }
                } catch (Exception e) {
                    request.setAttribute("login", login);
                    request.setAttribute("password", password);
                    request.setAttribute("name", name);
                    request.setAttribute("email", email);
                    request.setAttribute("tel", tel);
                    request.setAttribute("message", "电话号码只能为数字！");
                    request.getRequestDispatcher("upuser.jsp").forward(request, response);
                }
            }
        }
    }

    public void init() throws ServletException {
       
    }

}
